﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Diagnostics.Contracts;

namespace YasharEl.Infrastructure.ObjectFactory.ServiceModel
{
    public class DependencyContainerServiceHost : ServiceHost
    {
        private readonly IDependencyResolver resolver;
        private readonly IDependencyRegistrar registrar;

        public DependencyContainerServiceHost(Type serviceType,
            IDependencyResolver resolver, IDependencyRegistrar registrar,
            params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
            Contract.Requires<ArgumentNullException>(resolver != null, "resolver could not be null.");
            Contract.Requires<ArgumentNullException>(registrar != null, "registrar could not be null.");

            this.resolver = resolver;
            this.registrar = registrar;
        }

        protected override void OnOpen(TimeSpan timeout)
        {
            Description.Behaviors.Add(new DependencyContainerServiceBehavior(resolver, registrar));
            base.OnOpen(timeout);
        }
    }
}
